package com.physics.mps.module.mybatisHandler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.physics.mps.common.util.LoginUtil;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @author mcsoft
 * @date 2023/09/21 10:11
 **/
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        this.setFieldValByName("createTime", LocalDateTime.now(), metaObject);
        this.strictInsertFill(metaObject, "createBy", String.class, LoginUtil.getUsername());
        if (metaObject.hasSetter("updateTime")) {
            this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        // 对于更新操作的自动填充，如果需要的话
        this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
        this.strictUpdateFill(metaObject, "updateBy", String.class, LoginUtil.getUsername());
    }
}
